<template>
  <div>
    <!--面包屑导航-->
    <Breadcrumb>
      <template #firstTitle>
        <span>用户管理</span>
      </template>
      <template #secondTitle>
        <span>用户列表</span>
      </template>
    </Breadcrumb>

    <!--卡片视图-->
    <el-card>
      <!--搜索-->
      <el-row >
        <el-col :span="8">
          <el-input placeholder="请输入内容" clearable v-model="queryInfo.query" @clear="getOrderList">
            <el-button slot="append" icon="el-icon-search" @click="getOrderList"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!--订单列表-->
      <el-table
        border
        stripe
        :data="orderList">
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="order_number" label="订单编号"></el-table-column>
        <el-table-column prop="order_price" label="订单价格"></el-table-column>
        <el-table-column prop="pay_status" label="是否付款">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.pay_status === '1'">已付款</el-tag>
            <el-tag type="danger" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货"></el-table-column>
        <el-table-column prop="create_time" label="下单时间">
          <template slot-scope="scope">
            {{scope.row.create_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120px">
          <template slot-scope="scope">
            <!--修改按钮-->
            <el-tooltip class="item" effect="dark" content="修改地址" placement="top" :enterable="false">
              <el-button type="primary" icon="el-icon-edit" size="mini" @click="showAddress(scope.row)"></el-button>
            </el-tooltip>
            <!--物流信息按钮-->
            <el-tooltip class="item" effect="dark" content="物流信息" placement="top" :enterable="false">
              <el-button type="success" icon="el-icon-location" size="mini" @click="showLogistics(scope.row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!--分页-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>

    <!--修改地址对话框-->
    <el-dialog
      title="修改地址"
      :visible.sync="addressDialogVisible"
      @close="addressDialogClosed"
      width="50%">
      <!--内容主体-->
      <el-form
        status-icon
        :model="addressForm"
        :rules="addressFormRules"
        ref="addressFormRef"
        label-width="100px">
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader
            v-model="addressForm.address1"
            :options="cityData"
            :props="cityProps"
            @change="cityChange"
            clearable></el-cascader>
        </el-form-item>

        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="addressForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <!--内容底部-->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addressDialogVisible = false">取 消</el-button>
        <el-button type="primary" >确 定</el-button>
      </span>
    </el-dialog>

    <!--查看物流进度对话框-->
    <el-dialog
      title="查看物流进度"
      :visible.sync="logisticsDialogVisible"
      @close="logisticsDialogClosed"
      width="50%">
      <!--内容主体-->
      <el-timeline :reverse="true">
        <el-timeline-item
          v-for="(activity, index) in logisticsList"
          :key="index"
          :timestamp="activity.time">
          {{activity.context}}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
  import Breadcrumb from '@/components/common/Breadcrumb.vue'
  import cityData from './citydata'
  export default {
    name: "OrdersList",
    data() {
      return {
        orderList: [],  //订单列表
        queryInfo: {  // 获取订单列表的参数对象
          query: '',
          pagenum: 1, // 当前的页数
          pagesize: 5 // 当前每页显示多少条数据
        },
        total: 0,
        addressDialogVisible: false,
        addressForm: {
          address1: [],
          address2: ''
        },
        addressFormRules: {
          address1: [
            {required: true, message: '请选择省市区县', trigger: 'change'}
          ],
          address2: [
            {required: true, message: '请输入详细地址', trigger: 'blur'}
          ]
        },
        cityData: cityData,
        cityProps: {  //指定级联选择器的配置对象
          expandTrigger: 'hover',
          value: 'value',
          label: 'label',
          children: 'children'
        },
        logisticsDialogVisible: false,
        logisticsList: [],
      }
    },
    components: {
      Breadcrumb
    },
    created() {
      this.getOrderList();
    },
    methods: {
      // 获取订单列表
      async getOrderList() {
        const {data: res} = await this.$http.get('orders', {params: this.queryInfo});
        // console.log(res);
        if (res.meta.status !== 200) {
          this.$message.error(`获取订单列表数据: ${res.meta.msg}`);
          return;
        }
        this.orderList = res.data.goods;
        this.total = res.data.total;
      },
      // 监听pageSize改变
      handleSizeChange(newSize) {
        // console.log(`每页 ${newSize} 条`);
        this.queryInfo.pagesize = newSize;
        this.getOrderList();
      },
      // 监听页码值的改变
      handleCurrentChange(newPage) {
        // console.log(`当前页: ${newPage}`);
        this.queryInfo.pagenum = newPage;
        this.getOrderList();
      },
      // 展示修改地址对话框
      showAddress(row){
        this.addressDialogVisible = true;
      },
      // 监听修改地址对话框关闭
      addressDialogClosed(){
        this.$refs.addressFormRef.resetFields();
      },
      // 省市区/县改变
      cityChange(){

      },
      // 展示物流信息对话框
      async showLogistics(row){
        const {data: res} = await this.$http.get(`/kuaidi/1106975712662`);
        // console.log(res);
        if (res.meta.status !== 200) {
          this.$message.error(`获取物流信息: ${res.meta.msg}`);
          return;
        }
        this.logisticsList = res.data;
        this.logisticsDialogVisible = true;
      },
      logisticsDialogClosed(){

      }
    }
  }
</script>

<style scoped>
  .el-cascader{
    width: 100%;
  }
</style>
